

const uuid = require("uuid");
export function UUID() {
    return uuid.v1();
}

export function success(data = {}) {
    var ret = {
        errcode: 0,
        errmsg: {},
    }
    ret.errmsg = Object.assign(ret.errmsg, data);
    return ret;
}

export function failed(errcode = -100, data = {}) {
    var ret = {
        errcode: errcode,
        errmsg: data
    };
    return ret;
}

var Redis = require('ioredis');
var redis = new Redis();
// const redisGet = util.promisify(redis.get);

export function SetCache(name: string, val: any, expr?: any) {
    if (!expr) expr = 24 * 3600;
    if(typeof val == 'object'){
        val = JSON.stringify(val);
    }
    redis.set(name, val, 'EX', expr);
}
export async function GetCache(name: string): Promise<any> {
    try{
        let ret = await redis.get(name);
        if(ret[0] == '{' || ret[0] == '['){
            return JSON.parse(ret);
        }
        else{
            return ret;
        }
    }
    catch(e){
    }
}

